package com.zikao.article.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zikao.article.entity.PraiseEntity;
import com.zikao.article.service.PraiseService;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.R;



/**
 * @author likaisheng
 * @email 1805318928@qq.com
 * @date 2021-02-21 10:55:38
 */
@Controller
@Slf4j
@RequestMapping("article/praise")
public class PraiseController {
    @Autowired
    private PraiseService praiseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = praiseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 按参数获取点赞信息
     */
    @RequestMapping("/selectByMap")
    @ResponseBody
    public R selectByMap(@RequestBody Map<String, Object> params){
        List<PraiseEntity> praiseList = praiseService.selectByMap(params);
        return R.ok().put("praiseList", praiseList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Long id){
		PraiseEntity praise = praiseService.getById(id);

        return R.ok().put("praise", praise);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody PraiseEntity praise){
		praiseService.save(praise);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody PraiseEntity praise){
		praiseService.updateById(praise);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/removeObj")
    @ResponseBody
    public R removeObj(@RequestBody PraiseEntity praise){
        Map<String,Object> praiseMap = new HashMap<>();
        try{
            praiseMap.put("article_id",praise.getArticleId());
            praiseMap.put("user_id",praise.getUserId());
            praiseService.removeByMap(praiseMap);
        }catch (Exception e){
            log.error("程序出现错误-removeObj",e);
            return R.error();
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids){
		praiseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
